package com.example.demo.controller;


import com.example.demo.service.SubscribeService;
import com.example.demo.tools.Constant;
import com.example.demo.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/subscribe")
public class SubscribeController {
    // 注入 SubscribeService 依赖
    @Autowired
    private SubscribeService subscribeService;


    @RequestMapping("/add")
    public ResponseBodyMessage<Boolean> add (@RequestParam("email") String email) {
        // 非空判断
        if (StringUtils.hasLength(email)) {
            // 调用 SubscribeService.addSubscribe() 处理请求
            return subscribeService.add(email);
        } else {
            // 参数错误
            return new ResponseBodyMessage<>(400, "请求参数错误",false);
        }
    }

    @RequestMapping("/delete")
    public String delete (@RequestParam("email") String email, HttpServletResponse response) {
        // 非空判断
        response.setContentType("text/html;charset=utf-8");
        if (StringUtils.hasLength(email)) {
            // 调用 subscribeService.delete() 处理请求
            return Constant.EMAIL_FIRST + subscribeService.delete(email).getMessage() + Constant.EMAIL_Second_Another;
        } else {
            // 参数错误
            return Constant.EMAIL_FIRST + "请求参数错误" + Constant.EMAIL_Second_Another;
        }
    }
}
